; void normalizar_local_asm (
; 	unsigned char *src,
; 	unsigned char *dst,
; 	int m,
; 	int n,
; 	int row_size
; );

global normalizar_local_asm

section .data
	MASCARA_PARA_NO_DIVIDIR: dq 0101010101010101h
section .text

normalizar_local_asm:
	;; TODO: Implementar
	push rbp
	mov rbp, rsp
	push rbx
	push r12
	push r13
	push r14
	push r15

	pxor xmm7, xmm7

	mov r14, rdx
	xor rdx, rdx
	xor rax, rax
	mov eax, ecx
	mov ebx, 14
	div ebx

	mov r15, rax					;divido por la cantidad de pixels simultaneos que proceso
	mov r13, rdx				;calculo el resto
	mov rdx, r14
	dec rdx

	movq xmm5, [MASCARA_PARA_NO_DIVIDIR]
	pslldq xmm1, 8
	movq xmm5, [MASCARA_PARA_NO_DIVIDIR]

	xor r12, r12
	inc r12

	lea rsi, [rsi + r8]
	inc rsi
	cicloFilas:
		xor r11, r11
		cicloColumnas:
			lea r10, [rdi + r11*8]
			lea r10, [r10 + r11*4]
			lea r10, [r10 + r11*2]
			movdqu xmm0, [r10]		;xmm0 = xx|p14|p13|p12|p11|p10|p9|p8|p7|p6|p5|p4|p3|p2|p1|xx
			movdqu xmm4, xmm0

			lea r10, [r10 + r8]
			movdqu xmm1, [r10]
			movdqu xmm3, xmm1

			lea r10, [r10 + r8]
			movdqu xmm2, [r10]

			pmaxub xmm0, xmm1
			pmaxub xmm0, xmm2

			pminub xmm4, xmm1
			pminub xmm4, xmm2

			movdqu xmm1, xmm0
			psrldq xmm1, 1
			movdqu xmm2, xmm0
			pslldq xmm2, 1

			pmaxub xmm0, xmm1
			pmaxub xmm0, xmm2

			movdqu xmm1, xmm4
			psrldq xmm1, 1
			movdqu xmm2, xmm4
			pslldq xmm2, 1			

			pminub xmm4, xmm1
			pminub xmm4, xmm2

			pcmpeqb xmm3, xmm0
			pand xmm3, xmm5
			paddb xmm3, xmm4

			psrldq xmm3, 1
			lea r10, [rsi + r11*8]
			lea r10, [r10 + r11*4]
			lea r10, [r10 + r11*2]
			movdqu [r10], xmm3
;			movq [r10], xmm3

;			psrldq xmm3, 8
;			add r10, 8
;			movd [r10], xmm3
;			psrldq xmm3, 4
;			add r10, 4
;			pextrw eax, xmm3, 0
;			mov [r10], ax
		inc r11
		cmp r11, r15
		jl cicloColumnas

		cmp r13, 0
		je se_proceso_linea_completa

;		mov r11, rcx
;		sub r11, r13
		dec r11
;
;		mov r14, r11
;		procesar_lo_que_queda:
			lea r10, [rdi + r11*8]
			lea r10, [r10 + r11*4]
			lea r10, [r10 + r11*2]
			add r10, r13
			movdqu xmm0, [r10]		;xmm0 = xx|p14|p13|p12|p11|p10|p9|p8|p7|p6|p5|p4|p3|p2|p1|xx
			movdqu xmm4, xmm0

			lea r10, [r10 + r8]
			movdqu xmm1, [r10]
			movdqu xmm3, xmm1

			lea r10, [r10 + r8]
			movdqu xmm2, [r10]

			pmaxub xmm0, xmm1
			pmaxub xmm0, xmm2

			pminub xmm4, xmm1
			pminub xmm4, xmm2

			movdqu xmm1, xmm0
			psrldq xmm1, 1
			movdqu xmm2, xmm0
			pslldq xmm2, 1

			pmaxub xmm0, xmm1
			pmaxub xmm0, xmm2

			movdqu xmm1, xmm4
			psrldq xmm1, 1
			movdqu xmm2, xmm4
			pslldq xmm2, 1			

			pminub xmm4, xmm1
			pminub xmm4, xmm2

			pcmpeqb xmm3, xmm0
			pand xmm3, xmm5
			paddb xmm3, xmm4

			psrldq xmm3, 1
			lea r10, [rsi + r11*8]
			lea r10, [r10 + r11*4]
			lea r10, [r10 + r11*2]
			add r10, r13
			movdqu [r10], xmm3
;			movq [r10], xmm3

;			psrldq xmm3, 8
;			add r10, 8
;			movd [r10], xmm3
;			psrldq xmm3, 4
;			add r10, 4
;			pextrw eax, xmm3, 0
;			mov [r10], ax
;		inc r14
;		cmp r14, rcx
;		jl procesar_lo_que_queda
		se_proceso_linea_completa:

		lea rdi, [rdi + r8]
		lea rsi, [rsi + r8]
	inc r12
	cmp r12, rdx
	jl cicloFilas

	pop r15
	pop r14
	pop r13
	pop r12
	pop rbx
	pop rbp
	ret
